package org.tuoren.official.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.bo.ValuesBo;
import org.tuoren.official.domain.vo.ValuesVo;
import org.tuoren.official.service.IValuesService;

/**
 * 集团价值观信息
 *
 * @author wl
 * @date 2024-11-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/values")
public class ValuesController extends BaseController {

    private final IValuesService valuesService;

    /**
     * 前台-查询价值观信息
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<ValuesVo> frontList() {
        return valuesService.frontList();
    }

    /**
     * 查询集团价值观信息列表
     */
    @SaCheckPermission("official:values:list")
    @GetMapping("/list")
    public TableDataInfo<ValuesVo> list(ValuesBo bo, PageQuery pageQuery) {
        return valuesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出集团价值观信息列表
     */
    @SaCheckPermission("official:values:export")
    @Log(title = "集团价值观信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ValuesBo bo, HttpServletResponse response) {
        List<ValuesVo> list = valuesService.queryList(bo);
        ExcelUtil.exportExcel(list, "集团价值观信息", ValuesVo.class, response);
    }

    /**
     * 获取集团价值观信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:values:query")
    @GetMapping("/{id}")
    public R<ValuesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(valuesService.queryById(id));
    }

    /**
     * 新增集团价值观信息
     */
    @SaCheckPermission("official:values:add")
    @Log(title = "集团价值观信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ValuesBo bo) {
        return toAjax(valuesService.insertByBo(bo));
    }

    /**
     * 修改集团价值观信息
     */
    @SaCheckPermission("official:values:edit")
    @Log(title = "集团价值观信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ValuesBo bo) {
        return toAjax(valuesService.updateByBo(bo));
    }

    /**
     * 删除集团价值观信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:values:remove")
    @Log(title = "集团价值观信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(valuesService.deleteWithValidByIds(List.of(ids), true));
    }
}
